package com.txzt.gaea.business.fund.service.impl;

import com.txzt.gaea.business.fund.domain.model.FundBudget;
import com.txzt.gaea.business.fund.service.IFundBudgetService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.enums.BudgetStatus;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.FundStatus;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.flow.vo.FlowBizStepVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FundBudgetServiceImpl extends BaseServiceImpl<FundBudget> implements IFundBudgetService {

    @Autowired
    private FlowService flowService;

    @Override
    public Map getFundSum(){
        List<Map> list = sqlManager.select("fundBudget.getFundSum",Map.class,null);
        return list.get(0);
    }

    @Override
    public FundBudget selectFundBudgetById(Long id) {
        FundBudget fundBudget = sqlManager.single(FundBudget.class,id);
        if (null == fundBudget) {
            return null;
        }
        List<FlowBizStepVo> list = flowService.getFlowBizStep(fundBudget.getFlowId());
        fundBudget.setFlowStep(list);
        fundBudget.setCheck(TranslateUtil.getCheck(list));
        return fundBudget;
    }

    @Override
    public int addFundBudget(FundBudget fundBudget) {
        long count = sqlManager.query(FundBudget.class)
                .andEq("budget_name",fundBudget.getBudgetName())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode()).count();
        if (count > 0) {
            return 9;
        }
        fundBudget.setBudgetStatus(BudgetStatus.INIT.getCode());
        fundBudget.setFundStatus(FundStatus.INIT.getCode());
        fundBudget.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        return sqlManager.insertTemplate(fundBudget);
    }

    @Override
    public int editFundBudget(FundBudget fundBudget) {
        long count = sqlManager.query(FundBudget.class)
                .andEq("budget_name",fundBudget.getBudgetName())
                .andEq("is_delete",IsDeleteEnum.NOTDELETE.getCode())
                .andNotEq("id",fundBudget.getId())
                .count();
        if (count > 0) {
            return 9;
        }
        return sqlManager.updateTemplateById(fundBudget);
    }

    @Override
    public int submitFundBudget(Long id) {
        //开启流程
        Long flowBizId = flowService.startFlow(id, "tb_fund_budget","budget_status",
                BudgetStatus.REJECT.getCode(), BudgetStatus.PASS.getCode(), FlowFlagEnum.BUDGET_MANAGEMENT.getCode());
        FundBudget fundBudget = new FundBudget();
        fundBudget.setId(id);
        fundBudget.setBudgetStatus(BudgetStatus.CHECK.getCode());
        fundBudget.setFlowId(flowBizId);
        return sqlManager.updateTemplateById(fundBudget);
    }

    @Override
    public List<FundBudget> getHandledList() {
        return sqlManager.query(FundBudget.class).andEq("fund_status", FundStatus.FINISH.getCode()).andEq("is_delete", IsDeleteEnum.NOTDELETE.getCode()).select();
    }
}
